home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / Display Manager Sample / Source / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1995-12-19  |  10.3 KB  |  458 lines  |  [TEXT/MPCC]

  1. /***********************************************************************
  2. #
  3. #        Windows.c
  4. #
  5. #        This segment handles the window creation, close, updates,
  6. #
  7. #        Author: Michael Marinkovich
  8. #                Apple Developer Technical Support
  9. #
  10. #
  11. #        Modification History: 
  12. #
  13. #            6/4/95        MWM     Initial coding                     
  14. #            10/12/95    MWM        cleaned up
  15. #
  16. #        Copyright © 1992-95 Apple Computer, Inc., All Rights Reserved
  17. #
  18. #
  19. ***********************************************************************/
  20.  
  21. #include <Windows.h>
  22.  
  23. #include "App.h"
  24. #include "Proto.h"
  25.  
  26.  
  27. extern Boolean        gHasAbout;        // have an about box?
  28. extern short        gWindCount;
  29.  
  30.  
  31. //----------------------------------------------------------------------
  32. //
  33. //    CreateWindow - create a window from the info passed in. Will try to 
  34. //                   load from resource if resID is supplied.
  35. //
  36. //----------------------------------------------------------------------
  37.  
  38. WindowPtr CreateWindow(short resID, void *wStorage, Rect *bounds,  Str255 title,
  39.                         Boolean visible, short procID , short kind,
  40.                         WindowRef behind, Boolean goAwayFlag, long refCon)
  41. {
  42.     OSErr            err = nil;
  43.     WindowRef        newWindow = nil;
  44.     
  45.     
  46.     if (resID != nil)         // if res id isn't nil then load from disk
  47.         newWindow = GetNewWindow(resID, wStorage, behind);
  48.     else                    // otherwise make a new windowRecord
  49.          newWindow = NewWindow(wStorage, bounds, title, visible, 
  50.                                procID, behind, goAwayFlag, refCon);
  51.                                
  52.     if (newWindow != nil) {
  53.         NewWindowTitle(newWindow, title);
  54.         err = InitWindowProcs(newWindow, kind);
  55.         
  56.         if (err == noErr) {
  57.             SetPort(newWindow);
  58.             ShowWindow(newWindow);
  59.         }
  60.                 
  61.         // initialization of Document Record faild
  62.         // so kill the return window    
  63.         else {
  64.             newWindow = nil;
  65.             HandleError(err, false);
  66.         }        
  67.     }
  68.     
  69.     return newWindow;
  70.  
  71. }
  72.     
  73.  
  74. //----------------------------------------------------------------------
  75. //
  76. //    RemoveWindow - applications Doc window disposal routine.
  77. //                 
  78. //
  79. //----------------------------------------------------------------------
  80.  
  81. OSErr RemoveWindow( WindowRef window )
  82. {
  83.     OSErr            err = nil;
  84.     short            kind;
  85.     DocHnd            doc;
  86.     
  87.     
  88.     kind = GetWindKind( window );
  89.     if ( kind < kDocKind || kind > kAboutKind )
  90.         return -1; // not our window
  91.     
  92.     doc = (DocHnd)GetWRefCon( window );
  93.     if ( doc != nil ) {
  94.         switch( kind ) {
  95.             case kDocKind:
  96.                 DisposeGWorld((**doc).world);
  97.                 DisposeHandle( (Handle)doc );
  98.                 DisposeWindow( window );
  99.                 err = noErr;
  100.                 break;
  101.             
  102.             case kAboutKind:
  103.                 DisposeHandle( (Handle)doc );
  104.                 DisposeWindow( window );
  105.                 err = noErr;
  106.                 gHasAbout = false;    // allow new about box
  107.                 break;
  108.             
  109.             default:
  110.                 break;
  111.                     
  112.  
  113.         }    
  114.     }
  115.     
  116.     return err;
  117. }        
  118.  
  119.  
  120. //----------------------------------------------------------------------
  121. //
  122. //    NewWindowTitle - if supplied title is nil then title is set to 
  123. //                      global "gWindCount".
  124. //
  125. //----------------------------------------------------------------------
  126.  
  127. void NewWindowTitle(WindowRef window, Str255 str)
  128. {
  129.     Str255        catStr = "\pUntitled ";
  130.     Str255        newStr;
  131.     
  132.     
  133.     if (str == nil || StrLength(str) == 0) {
  134.         pstrcpy(newStr, catStr);
  135.         NumToString(gWindCount, catStr);
  136.         pstrcat(newStr, catStr);
  137.         gWindCount++;
  138.         
  139.         SetWTitle(window,newStr);
  140.     }
  141.     else
  142.         SetWTitle(window, str);
  143.  
  144. }
  145.  
  146.  
  147. //----------------------------------------------------------------------
  148. //
  149. //    InitWindowProcs - init a window with proper callback event. fills 
  150. //                       out custom procs for different windowkinds.
  151. //
  152. //----------------------------------------------------------------------
  153.  
  154. OSErr InitWindowProcs(WindowRef window, short windKind)
  155. {
  156.     OSErr            err = nil;
  157.     DocHnd            doc;
  158.     
  159.     doc = (DocHnd)NewHandle(sizeof(DocRec));
  160.     if (doc != nil) {
  161.         SetWRefCon(window, (long)doc);
  162.  
  163.         switch(windKind) {
  164.             case kDocKind:
  165.                 (**doc).idleProc        = DoIdle;
  166.                 (**doc).mMenuProc        = HandleMenuChoice;
  167.                 (**doc).inContentProc    = HandleContentClick;
  168.                 (**doc).inGoAwayProc    = nil;
  169.                 (**doc).inZoomProc        = HandleZoomClick;
  170.                 (**doc).inGrowProc        = HandleGrow;
  171.                 (**doc).keyProc            = nil;
  172.                 (**doc).activateProc    = DoActivate;
  173.                 (**doc).updateProc        = DrawWindow;    
  174.                 (**doc).hScroll         = nil;
  175.                 (**doc).vScroll            = nil;
  176.                 (**doc).world            = nil;
  177.                 (**doc).pict            = nil;
  178.                 (**doc).printer            = nil;
  179.                 (**doc).dirty            = false;
  180.                 
  181.                 InstallScrollBars(window, doc);
  182.  
  183.                 break;
  184.                 
  185.             case kDialogKind:
  186.                 break;
  187.  
  188.             case kAboutKind:
  189.                 (**doc).idleProc        = DoIdle;
  190.                 (**doc).mMenuProc        = HandleMenuChoice;
  191.                 (**doc).inContentProc    = nil;
  192.                 (**doc).inGoAwayProc    = nil;
  193.                 (**doc).inZoomProc        = nil;
  194.                 (**doc).inGrowProc        = nil;
  195.                 (**doc).keyProc            = nil;
  196.                 (**doc).activateProc    = nil;
  197.                 (**doc).updateProc        = DrawAboutWindow;    
  198.                 (**doc).hScroll         = nil;
  199.                 (**doc).vScroll            = nil;
  200.                 (**doc).world            = nil;
  201.                 (**doc).pict            = GetPicture(rAboutPictID);
  202.                 (**doc).printer            = nil;
  203.                 (**doc).dirty            = false;
  204.                 
  205.                 break;
  206.                 
  207.             default:
  208.                 err = 25;
  209.                 break;    
  210.         }
  211.         ((WindowPeek)window)->windowKind = windKind;
  212.  
  213.     }            
  214.     return err;
  215.  
  216. }
  217.  
  218.  
  219. //----------------------------------------------------------------------
  220. //
  221. //    PictToWorld - create a GWorld from a Pict. Size is determined by the
  222. //                  bounds of the pict. If the pict is nil then a default
  223. //                  bounds is used, in case of an empty window.
  224. //----------------------------------------------------------------------
  225.     
  226. GWorldPtr PictToWorld(PicHandle pict, OSErr *rtnErr)
  227. {
  228.     OSErr            err;
  229.     GWorldPtr        oldWorld;
  230.     GWorldPtr        theWorld = nil;
  231.     GDHandle        oldGD;
  232.     PixMapHandle    thePix;
  233.     Rect            bounds;
  234.     
  235.     GetGWorld(&oldWorld, &oldGD);
  236.     
  237.     if (pict != nil)
  238.         bounds = (**pict).picFrame;
  239.     else
  240.         SetRect(&bounds, 0, 0, 200, 200);    
  241.     
  242.     err = NewGWorld(&theWorld, 8,&bounds, nil, nil, 0L);
  243.     if (err == noErr && theWorld != nil) {
  244.         thePix = GetGWorldPixMap(theWorld);
  245.         if (LockPixels(thePix)) {
  246.             SetGWorld(theWorld, nil);
  247.             EraseRect(&bounds);
  248.             if (pict != nil)
  249.                 DrawPicture(pict, &bounds);
  250.             
  251.             UnlockPixels(thePix);
  252.         }
  253.         SetGWorld(oldWorld, oldGD);
  254.     }
  255.     
  256.     *rtnErr = err;
  257.     
  258.     return theWorld;
  259. }
  260.  
  261.         
  262. //----------------------------------------------------------------------
  263. //
  264. //    DrawWindow - custom proc that is called to update window contents.
  265. //                 
  266. //
  267. //----------------------------------------------------------------------
  268.  
  269. void DrawWindow(WindowRef window, void *refCon)
  270. {
  271.     DocHnd            doc;
  272.     GWorldPtr        theWorld;
  273.     PixMapHandle    thePix;
  274.     Rect            cRect;
  275.     Rect            bounds;
  276.     
  277.     doc = (DocHnd)GetWRefCon(window);    
  278.     if (doc != nil) {
  279.         SetPort(window);
  280.         GetContRect(window, &cRect);
  281.         ClipRect(&cRect);
  282.         
  283.         theWorld = (**doc).world;
  284.  
  285.         if (theWorld != nil ) {
  286.             bounds = theWorld->portRect;
  287.             OffsetRect(&bounds, -GetCtlValue((**doc).hScroll), 
  288.                        -GetCtlValue((**doc).vScroll));
  289.             thePix = GetGWorldPixMap(theWorld);
  290.             if (LockPixels(thePix)) {
  291.                 CopyBits((BitMap *) *thePix, &window->portBits,
  292.                          &theWorld->portRect, &bounds, srcCopy, nil);
  293.                 
  294.                 UnlockPixels(thePix);
  295.             }             
  296.                         
  297.         }
  298.         else
  299.             EraseRect(&cRect);    
  300.  
  301.         ClipRect(&window->portRect);
  302.         DrawGrowIcon(window);
  303.         UpdateControls(window, window->visRgn);
  304.     }
  305.  
  306. }
  307.  
  308.  
  309. //----------------------------------------------------------------------
  310. //
  311. //    DrawAboutWindow - custom proc that is called to update about window.
  312. //                 
  313. //
  314. //----------------------------------------------------------------------
  315.  
  316. void DrawAboutWindow( WindowRef window, void *refCon )
  317. {    
  318.     DocHnd        doc;
  319.     
  320.     doc = (DocHnd)GetWRefCon(window);    
  321.     if ( (**doc).pict != nil ) 
  322.         DrawPicture( (**doc).pict, &window->portRect );
  323.  
  324. }
  325.  
  326.  
  327. //----------------------------------------------------------------------
  328. //
  329. //    DoResizeWindow - custom proc that is called to update window.
  330. //                 
  331. //
  332. //----------------------------------------------------------------------
  333.  
  334. void DoResizeWindow (WindowRef window) 
  335. {
  336.     DocHnd            doc;
  337.     ControlHandle    hCtl, vCtl;
  338.     RgnHandle        tempRgn;
  339.     RgnHandle        oldCtlRgn;
  340.     short            max, oldCntlVal;
  341.     short            pV, wV;
  342.     short             pH, wH;
  343.     Rect            hSRect;
  344.     Rect            newDocRect;
  345.     Rect            pictRect;
  346.     Rect            paneRect;
  347.  
  348.     doc = (DocHnd)GetWRefCon(window);
  349.     if (doc != nil) {
  350.         pictRect = (**doc).world->portRect;
  351.         newDocRect = (**window->visRgn).rgnBBox;
  352.  
  353.         hCtl = (**doc).hScroll;
  354.         vCtl = (**doc).vScroll;
  355.         
  356.         hSRect = (**hCtl).contrlRect;
  357.         hSRect.right += kScrollWidth;
  358.         ClipRect(&window->portRect);
  359.         RectRgn(oldCtlRgn = NewRgn(), &hSRect);
  360.         RectRgn(tempRgn = NewRgn(), &(**vCtl).contrlRect);
  361.         UnionRgn(oldCtlRgn, tempRgn, oldCtlRgn);
  362.         EraseRgn(oldCtlRgn);
  363.         
  364.         (**hCtl).contrlVis = 0;
  365.         (**vCtl).contrlVis = 0;
  366.  
  367.         paneRect = newDocRect;
  368.         paneRect.bottom -= kScrollWidth;
  369.         paneRect.right -= kScrollWidth;
  370.         
  371.         MoveControl(hCtl, paneRect.left - 1, paneRect.bottom);    
  372.         MoveControl(vCtl, paneRect.right, paneRect.top - 1);
  373.             
  374.         SizeControl(hCtl, 2+paneRect.right - paneRect.left, kScrollWidth + 1);    
  375.         SizeControl(vCtl, kScrollWidth + 1, 2+paneRect.bottom - paneRect.top);
  376.         
  377.         (**hCtl).contrlVis = 255;
  378.         (**vCtl).contrlVis = 255;
  379.         
  380.         pV = pictRect.bottom - pictRect.top;
  381.         wV = paneRect.bottom - paneRect.top;
  382.         if (wV >= pV)
  383.             max = 0;
  384.         else
  385.             max = ((MAX (0, pV - wV)) / kScrollDelta) + 1;
  386.             
  387.         oldCntlVal = GetControlValue(vCtl);
  388.         SetControlMinimum(vCtl, 0);
  389.         SetControlMaximum(vCtl, max);
  390.         SetControlValue(vCtl, oldCntlVal );
  391.         
  392.         HiliteControl(vCtl, (max == 0) ? (255):(0));    
  393.  
  394.         pH = pictRect.right - pictRect.left;
  395.         wH = paneRect.right - paneRect.left;
  396.         if (wH >= pH)
  397.             max = 0 ;
  398.         else
  399.             max = ((MAX (0, pH - wH )) / kScrollDelta) +1;
  400.         oldCntlVal = GetControlValue(hCtl);
  401.         SetControlMinimum(hCtl, 0);
  402.         SetControlMaximum(hCtl, max) ;
  403.         SetControlValue(hCtl, oldCntlVal);
  404.         
  405.         HiliteControl(hCtl, (max == 0) ? (255):(0));    
  406.                 
  407.         DisposeRgn(oldCtlRgn);
  408.         DisposeRgn(tempRgn);
  409.         AdjustScrollValues(window);
  410.         InvalRect(&window->portRect);
  411.  
  412.     }
  413.  
  414. }
  415.  
  416.  
  417. //----------------------------------------------------------------------
  418. //
  419. //    GetWindKind - returns the windowkind.
  420. //                 
  421. //
  422. //----------------------------------------------------------------------
  423.  
  424. short GetWindKind(WindowRef window)
  425. {
  426.  
  427.     return ((WindowPeek)window)->windowKind;
  428.  
  429. }
  430.  
  431.  
  432. //----------------------------------------------------------------------
  433. //
  434. //    GetIsAppWindow - is the window a 'userKind'.
  435. //                 
  436. //
  437. //----------------------------------------------------------------------
  438.  
  439. Boolean GetIsAppWindow(WindowRef window)
  440. {
  441.     return (GetWindKind(window) == kDocKind);
  442.  
  443. }
  444.  
  445.  
  446. //----------------------------------------------------------------------
  447. //
  448. //    GetIsAboutWindow - is the window an about box.
  449. //                 
  450. //
  451. //----------------------------------------------------------------------
  452.  
  453. Boolean GetIsAboutWindow(WindowRef window)
  454. {
  455.     return (GetWindKind(window) == kAboutKind);
  456.     
  457. }
  458.